//
//  ItemVC4.m
//  喜马拉雅FM
//
//  Created by dllo on 16/1/22.
//  Copyright © 2016年 lanou.com. All rights reserved.
//

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

#import "ItemVC4.h"
#import "ItemVC4TableViewCell.h"
#import "MJRefresh.h"

#import "NetworkHandler.h"
#import "ModelForDownLoad.h"
#import "SecendViewController.h"

#import "ModelForListen.h"

@interface ItemVC4 ()<UITableViewDataSource, UITableViewDelegate,NetworkHandlerDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *arrDataForDownLoad;


@end

@implementation ItemVC4

- (void)viewWillAppear:(BOOL)animated {

  

}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"收藏推荐";
    self.view.backgroundColor = [UIColor whiteColor];

    [self handleDataForHot];
    [self createSegment];
}


- (void)handleDataForHot {


     [NetworkHandler handlerJSONWithURL:@"http://mobile.ximalaya.com/mobile/discovery/v1/category/album?calcDimension=hot&categoryId=0&device=android&pageId=1&pageSize=20&status=0&tagName=" delegate:self];


}

- (void)createSegment {


    UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:@[@"最 火",@"最 新",@"经 典"]];
    segment.frame = CGRectMake(10, 74, WIDTH - 20, 40);
    [self.view addSubview:segment];
    segment.tintColor = [UIColor redColor];
    [segment release];
    
    /** 默认选中的item. */
    segment.selectedSegmentIndex = 0;
      [self.tableView reloadData];
    
    /** 添加事件. */
    [segment addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];

}

- (void)segmentAction:(UISegmentedControl *)segment {

    switch (segment.selectedSegmentIndex) {
        case 0: [NetworkHandler handlerJSONWithURL:@"http://mobile.ximalaya.com/mobile/discovery/v1/category/album?calcDimension=hot&categoryId=0&device=android&pageId=1&pageSize=20&status=0&tagName=" delegate:self];

            break;
        case 1:
            [NetworkHandler handlerJSONWithURL:@"http://mobile.ximalaya.com/mobile/discovery/v1/category/album?calcDimension=recent&categoryId=0&device=android&pageId=1&pageSize=20&status=0&tagName=" delegate:self];
            break;
        case 2:
            [NetworkHandler handlerJSONWithURL:@"http://mobile.ximalaya.com/mobile/discovery/v1/category/album?calcDimension=classic&categoryId=0&device=android&pageId=1&pageSize=20&status=0&tagName="delegate:self];
            break;
        default:
            break;
    }
}

- (void)handlerDidComplete:(id)result {

    self.arrDataForDownLoad = [NSMutableArray array];
    
    NSDictionary *tempDic = [NSDictionary dictionaryWithDictionary:result];
    NSArray *tempArr = [tempDic objectForKey:@"list"];
    for (NSDictionary *dic in tempArr) {
        /////////////////////
        ModelForListen *model = [[ModelForListen alloc]init];
        [model setValuesForKeysWithDictionary:dic];
        [self.arrDataForDownLoad addObject:model];
    }
  
    [self.tableView reloadData];
    [self createTableView];
}

#pragma mark createTableView
- (void)createTableView {

    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 114, WIDTH, HEIGHT - 114 - 49) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [self.tableView release];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[ItemVC4TableViewCell class] forCellReuseIdentifier:@"poolForItemVC4TableViewCell"];
    
}

#pragma mark tableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.arrDataForDownLoad.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    ModelForListen *model = [self.arrDataForDownLoad objectAtIndex:indexPath.row];
    ItemVC4TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForItemVC4TableViewCell"];
    [cell passModelForDownLoad:model];
    return cell;

}

#pragma mark tableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 100;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    ModelForListen *model = [self.arrDataForDownLoad objectAtIndex:indexPath.row];
    SecendViewController *secend = [[SecendViewController alloc]init];
    secend.stringForAlbumID = model.albumId;
    secend.modelForListen = model;
    [self.navigationController pushViewController:secend animated:YES];
    
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

@end
